from fastapi import FastAPI
import uvicorn
from typing import Optional
from pydantic import BaseModel
from enum import Enum


app = FastAPI()


# 枚举字段
class Gender(str, Enum):
    male = "male"
    famale = "famale"


# 请求体验证
class UserModel(BaseModel):
    username:str
    gender:Gender
    decription:Optional[str] = "default"

class A():
    ...


# 路径参数
@app.get("/hello/{id}") 
async def hello_world(id:int):
    return {"data":"hello world", "code":200}

# 查询参数
@app.post("/users")
async def create_user(user_model:UserModel):
    user_dict = user_model.model_dump()
    return user_dict

# 查询参数
@app.get("/users")
async def get_user(page:int, page_size:Optional[int]=10):
    return {"data":f"page:{page}, page_size:{page_size}"}


# 查询参数 + 路径参数
@app.get("/users/{user_id}")
async def get_user(page:int,user_id:int, page_size:Optional[int]=10 ):
    return {"data":f"user_id:{user_id}, page:{page}, page_size:{page_size} "}    


if __name__ == "__main__":

    uvicorn.run("main:app", reload=True)